package com.dianping.cat.consumer.event.model.transform;

import com.dianping.cat.consumer.event.model.IEntity;
import com.dianping.cat.consumer.event.model.IVisitor;
import com.dianping.cat.consumer.event.model.entity.EventName;
import com.dianping.cat.consumer.event.model.entity.EventReport;
import com.dianping.cat.consumer.event.model.entity.EventType;
import com.dianping.cat.consumer.event.model.entity.GraphTrend;
import com.dianping.cat.consumer.event.model.entity.Machine;
import com.dianping.cat.consumer.event.model.entity.Range;
import com.dianping.cat.consumer.event.model.entity.StatusCode;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


























public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  52 */   public DefaultXmlBuilder() { this(false); }



/*  56 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  60 */     this.m_compact = compact;
/*  61 */     this.m_sb = sb;
/*  62 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  66 */     entity.accept(this.m_visitor);
/*  67 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  71 */     this.m_level--;

/*  73 */     indent();
/*  74 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


  protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  82 */     if (value == null) {
/*  83 */       return null;
    }

/*  86 */     String str = toString(value);
/*  87 */     int len = str.length();
/*  88 */     StringBuilder sb = new StringBuilder(len + 16);

/*  90 */     for (int i = 0; i < len; i++) {
/*  91 */       char ch = str.charAt(i);

/*  93 */       switch (ch) {
        case '<':
/*  95 */           sb.append("&lt;");
          break;
        case '>':
/*  98 */           sb.append("&gt;");
          break;
        case '&':
/* 101 */           sb.append("&amp;");
          break;
        case '"':
/* 104 */           if (!text) {
/* 105 */             sb.append("&quot;");
            break;
          }
        default:
/* 109 */           sb.append(ch);
          break;
      }

    }
/* 114 */     return sb.toString();
  }

  protected void indent() {
/* 118 */     if (!this.m_compact) {
/* 119 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 120 */         this.m_sb.append("   ");
      }
    }
  }


/* 126 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 130 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 134 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 138 */     indent();

/* 140 */     this.m_sb.append('<').append(name);

/* 142 */     int len = nameValues.length;

/* 144 */     for (int i = 0; i + 1 < len; i += 2) {
/* 145 */       Object attrName = nameValues[i];
/* 146 */       Object attrValue = nameValues[i + 1];

/* 148 */       if (attrValue != null) {
/* 149 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 153 */     if (dynamicAttributes != null) {
/* 154 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 155 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 159 */     if (text != null && closed) {
/* 160 */       this.m_sb.append('>');
/* 161 */       this.m_sb.append(escape(text, true));
/* 162 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 164 */       if (closed) {
/* 165 */         this.m_sb.append('/');
      } else {
/* 167 */         this.m_level++;
      }

/* 170 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 176 */     if (value instanceof String)
/* 177 */       return (String)value; 
/* 178 */     if (value instanceof Collection) {
/* 179 */       Collection<Object> list = (Collection)value;
/* 180 */       StringBuilder sb = new StringBuilder(32);
/* 181 */       boolean first = true;

/* 183 */       for (Object item : list) {
/* 184 */         if (first) {
/* 185 */           first = false;
        } else {
/* 187 */           sb.append(',');
        }

/* 190 */         if (item != null) {
/* 191 */           sb.append(item);
        }
      }

/* 195 */       return sb.toString();
/* 196 */     }  if (value.getClass().isArray()) {
/* 197 */       int len = Array.getLength(value);
/* 198 */       StringBuilder sb = new StringBuilder(32);
/* 199 */       boolean first = true;

/* 201 */       for (int i = 0; i < len; i++) {
/* 202 */         Object item = Array.get(value, i);

/* 204 */         if (first) {
/* 205 */           first = false;
        } else {
/* 207 */           sb.append(',');
        }

/* 210 */         if (item != null) {
/* 211 */           sb.append(item);
        }
      }

/* 215 */       return sb.toString();
    }

/* 218 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 222 */     if (text == null) {
      return;
    }

/* 226 */     indent();

/* 228 */     this.m_sb.append('<').append(name);

/* 230 */     int len = nameValues.length;

/* 232 */     for (int i = 0; i + 1 < len; i += 2) {
/* 233 */       Object attrName = nameValues[i];
/* 234 */       Object attrValue = nameValues[i + 1];

/* 236 */       if (attrValue != null) {
/* 237 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 241 */     this.m_sb.append(">");
/* 242 */     this.m_sb.append(escape(text, true));
/* 243 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 247 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 251 */     indent();

/* 253 */     this.m_sb.append('<').append(name).append(">");

/* 255 */     if (escape) {
/* 256 */       this.m_sb.append(escape(text, true));
    } else {
/* 258 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 261 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 265 */     if (date != null) {
/* 266 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 268 */     return null;
  }


  protected String toString(Number number, String format) {
/* 273 */     if (number != null) {
/* 274 */       return (new DecimalFormat(format)).format(number);
    }
/* 276 */     return null;
  }



  public void visitEventReport(EventReport eventReport) {
/* 282 */     startTag("event-report", null, new Object[] { "domain", eventReport.getDomain(), "startTime", toString(eventReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(eventReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 284 */     if (!eventReport.getDomainNames().isEmpty()) {
/* 285 */       for (String domain : eventReport.getDomainNames()) {
/* 286 */         tagWithText("domain", domain, new Object[0]);
      }
    }

/* 290 */     if (!eventReport.getIps().isEmpty()) {
/* 291 */       for (String ip : eventReport.getIps()) {
/* 292 */         tagWithText("ip", ip, new Object[0]);
      }
    }

/* 296 */     if (!eventReport.getMachines().isEmpty()) {
/* 297 */       for (Machine machine : eventReport.getMachines().values()) {
/* 298 */         machine.accept(this.m_visitor);
      }
    }

/* 302 */     endTag("event-report");
  }



/* 307 */   public void visitGraphTrend(GraphTrend graphTrend) { startTag("graph-trend", true, null, new Object[] { "duration", Integer.valueOf(graphTrend.getDuration()), "count", graphTrend.getCount(), "fails", graphTrend.getFails() }); }



  public void visitMachine(Machine machine) {
/* 312 */     startTag("machine", null, new Object[] { "ip", machine.getIp() });

/* 314 */     if (!machine.getTypes().isEmpty()) {
/* 315 */       for (EventType type : machine.getTypes().values()) {
/* 316 */         type.accept(this.m_visitor);
      }
    }

/* 320 */     endTag("machine");
  }


  public void visitName(EventName name) {
/* 325 */     startTag("name", null, new Object[] { "id", name.getId(), "totalCount", Long.valueOf(name.getTotalCount()), "failCount", Long.valueOf(name.getFailCount()), "failPercent", toString(Double.valueOf(name.getFailPercent()), "0.00"), "tps", toString(Double.valueOf(name.getTps()), "0.00") });

/* 327 */     element("successMessageUrl", name.getSuccessMessageUrl(), null, true);

/* 329 */     element("failMessageUrl", name.getFailMessageUrl(), null, true);

/* 331 */     if (!name.getRanges().isEmpty()) {
/* 332 */       for (Range range : name.getRanges().values()) {
/* 333 */         range.accept(this.m_visitor);
      }
    }

/* 337 */     if (name.getGraphTrend() != null) {
/* 338 */       name.getGraphTrend().accept(this.m_visitor);
    }

/* 341 */     if (!name.getStatusCodes().isEmpty()) {
/* 342 */       for (StatusCode statusCode : name.getStatusCodes().values()) {
/* 343 */         statusCode.accept(this.m_visitor);
      }
    }

/* 347 */     endTag("name");
  }



/* 352 */   public void visitRange(Range range) { startTag("range", true, null, new Object[] { "value", range.getValue(), "count", Integer.valueOf(range.getCount()), "fails", Integer.valueOf(range.getFails()) }); }




/* 357 */   public void visitStatusCode(StatusCode statusCode) { startTag("statusCode", true, null, new Object[] { "id", statusCode.getId(), "count", Long.valueOf(statusCode.getCount()) }); }



  public void visitType(EventType type) {
/* 362 */     startTag("type", null, new Object[] { "id", type.getId(), "totalCount", Long.valueOf(type.getTotalCount()), "failCount", Long.valueOf(type.getFailCount()), "failPercent", toString(Double.valueOf(type.getFailPercent()), "0.00"), "tps", toString(Double.valueOf(type.getTps()), "0.00") });

/* 364 */     element("successMessageUrl", type.getSuccessMessageUrl(), null, true);

/* 366 */     element("failMessageUrl", type.getFailMessageUrl(), null, true);

/* 368 */     if (!type.getNames().isEmpty()) {
/* 369 */       for (EventName name : type.getNames().values()) {
/* 370 */         name.accept(this.m_visitor);
      }
    }

/* 374 */     if (type.getGraphTrend() != null) {
/* 375 */       type.getGraphTrend().accept(this.m_visitor);
    }

/* 378 */     endTag("type");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/event/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */